import requests

url = 'https://gitee.com/'
headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.87 Safari/537.36'
}

# 构造cookies字典
cookies_str = 'Cookie: user_locale=zh-CN; oschina_new_user=false; remote_way=http; tz=Asia%2FShanghai; Hm_lvt_24f17767262929947cc3631f99bfd274=1595850600,1595857514,1596105637,1597091071; remember_user_token=BAhbCFsGaQMsvmNJIiIkMmEkMTAkOEJtUUdxUThJWWZ4TnI3WENZZmJCZQY6BkVUSSIVMTU5NzA5MTA3Ni4zMDUyNgY7AEY%3D--f09d69b3fd083ec851e6bb2153a00a0b53744805; Hm_lpvt_24f17767262929947cc3631f99bfd274=1597091080; gitee-session-n=WDE3V3hLaE93Nm00MkMxNTBoN3FyVUFpaVU4UmkwakFSdzV6OVNaRk05N2MyZ3hUMjlUU3NCb3JuTVNvNm90dk1XS1o3MUpKaUlpdnhuNEgwM0FjdWxBMEJTU3BvUlhyNUN1WkMxS0Nsam1aSXdxQTlIVHJYVWRiOTlHVmJvMVBiUmpnMjlOWXJudTRZQVJpSWtpblFmWTVHby9iL002TmNtdFlCN2tsZlpic0ZSN2ZDdC9CSjFSbXh5cFpOZmNHV3REZWVkRjc3eU1FRkJBRjA4eFFnV0Z5K2hRNWZlY1J2L2ExUnl4VkNJMDVJNElFYUZtQmZtTTBzYjU1bUpkSkF4TW05Z000Q3hVTTAvSkdJek52ZG00N3R4WDdRQ2pNZlh6OGVjaHBlUDEzOHQzbWJMd0Z5UE91a05aY0NlNkFlMlBjTXNlQlo3Yk1lZWFnbE9QWC9uSUVoKzlFN24xaFB4aGFLTklLTWFzPS0tK1hWNENleUhVdmdxVkc2dGFYR3VOQT09--585af30b0716147a50f3d02ba8751a2865f1f90d'

cookies_dict = {cookie.split('=')[0]:cookie.split('=')[-1] for cookie in cookies_str.split(";")}

print(cookies_dict)

# 请求头参数字典中携带cookie字符串
resp = requests.get(url, headers=headers, cookies=cookies_dict,timeout=3) # 超时时间为3s

# print(resp.text)

# cookieJar对象转换为cookies字典的方法
#1. 转换方法
# `cookies_dict = requests.utils.dict_from_cookiejar(response.cookies)`
#2. 其中response.cookies返回的就是cookieJar类型的对象
#3. `requests.utils.dict_from_cookiejar`函数返回cookies字典
print(type(resp.cookies))  #cookieJar对象
print(requests.utils.dict_from_cookiejar(resp.cookies))  #cookies字典

